/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package lu.med.praxiswahl.wahlumed.service.beans;

import javax.ejb.EJB;
import javax.ejb.Remote;
import javax.ejb.Stateless;

import lu.med.praxiswahl.wahlumed.dao.beans.daointerface.CodeRatesIntervalDAOInt;
import lu.med.praxiswahl.wahlumed.model.beans.CodeRatesIntervalTBL;
import lu.med.praxiswahl.wahlumed.service.beans.serviceinterface.CodeRateIntervalServiceInt;

/**
 * 
 * @author pkendzo
 */

@Stateless
@Remote({ CodeRateIntervalServiceInt.class })
public class CodeRateIntervalService implements CodeRateIntervalServiceInt {

	private CodeRatesIntervalTBL tmpCoRa;

	@EJB
	private CodeRatesIntervalDAOInt coRaIntDao;

	public CodeRateIntervalService() {

		tmpCoRa = new CodeRatesIntervalTBL();
	}

	@Override
	public int createCodeRateInterval(String fromDate, String toDate) {
		CodeRatesIntervalTBL tmp = coRaIntDao.getCodeRatesInterval();
		if (tmp != null) {
			this.deleteCodeRateInterval(tmp.getId());
		}
		CodeRatesIntervalTBL tmpCoRa_1 = new CodeRatesIntervalTBL();

		tmpCoRa_1.setFrom(fromDate);
		tmpCoRa_1.setToDate(toDate);

		coRaIntDao.persist(tmpCoRa_1);

		return RET_OPERATION_DONE_PROPERLY;
	}

	@Override
	public int deleteCodeRateInterval(Long id) {
		coRaIntDao.delete(id);
		return RET_OPERATION_DONE_PROPERLY;
	}

	@Override
	public void getCodeRateInteval(Long id) {
		tmpCoRa = coRaIntDao.retrieveCodeRateIntervalByID(id);

	}

	@Override
	public String getfromDate() {

		if (this.getAvailableInterval() == null) {
			return "";
		}
		CodeRatesIntervalTBL tmp = this.getCoderRateIntervalByID(this
				.getAvailableInterval());

		tmpCoRa.setFrom(tmp.getFromDate());
		return tmpCoRa.getFromDate();
	}

	@Override
	public String getToDate() {
		if (this.getAvailableInterval() == null) {
			return "";
		}
		CodeRatesIntervalTBL tmp = this.getCoderRateIntervalByID(this
				.getAvailableInterval());

		tmpCoRa.setToDate(tmp.getToDate());

		return tmpCoRa.getToDate();
	}

	@Override
	public CodeRatesIntervalTBL getCoderRateIntervalByID(Long id) {
		return coRaIntDao.retrieveCodeRateIntervalByID(id);
	}

	@Override
	public Long getAvailableInterval() {

		CodeRatesIntervalTBL tmp = coRaIntDao.getCodeRatesInterval();
		if (tmp == null)
			return null;
		return tmp.getId();
	}

	@Override
	public void setFromDate(String fromDate) {
		tmpCoRa.setFrom(fromDate);
		coRaIntDao.update(tmpCoRa);
	}

	@Override
	public void setToDate(String toDate) {
		tmpCoRa.setToDate(toDate);
		coRaIntDao.update(tmpCoRa);
	}

	public int updateCodeRateInterval(Long id, String fromDate, String toDate) {
		CodeRatesIntervalTBL tmpCodeInt = coRaIntDao
				.retrieveCodeRateIntervalByID(id);
		tmpCodeInt.setFrom(fromDate);
		tmpCodeInt.setToDate(toDate);

		coRaIntDao.update(tmpCodeInt);
		return 0;
	}
}
